import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
// import socket from '../socket/login'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 所有的任务列表
    list: [],
    // 文本框的内容
    inputValue: 'aaa',
    // 下一个Id
    nextId: 5,
    viewKey: 'all',
    isShowNav: false,
    // 扫码登录
    QRcode: '', // 二维码
    uuid: '', // 用户id
    ScanCodeShow: false, // 是否已经扫码
    tipsText: '请扫码', // 文字的提示
    overtimeShow: false, // 超时的判断
    successShow: false, // 扫码成功的判断
    WapSuccess: {}
  },
  mutations: {

    // 事务列表 ====================
    initList(state, list) {
      state.list = list
    },
    // 为 store 中的 inputValue 赋值
    setInputValue(state, val) {
      state.inputValue = val
    },
    // 添加列表项
    addItem(state) {
      const obj = {
        id: state.nextId,
        info: state.inputValue.trim(),
        done: false
      }
      state.list.push(obj)
      state.nextId++
      state.inputValue = ''
    },
    // 根据Id删除对应的任务事项
    removeItem(state, id) {
      // 根据Id查找对应项的索引
      const i = state.list.findIndex(x => x.id === id)
      // 根据索引，删除对应的元素
      if (i !== -1) {
        state.list.splice(i, 1)
      }
    },
    // 修改列表项的选中状态
    changeStatus(state, param) {
      const i = state.list.findIndex(x => x.id === param.id)
      if (i !== -1) {
        state.list[i].done = param.status
      }
    },
    // 清除已完成的任务
    cleanDone(state) {
      state.list = state.list.filter(x => x.done === false)
    },
    // 修改视图的关键字
    changeViewKey(state, key) {
      state.viewKey = key
    },

    // 扫码登录 ==========================

    //获取uuid 
    getUuid(state, uuid) {
      state.uuid = uuid
    },
    // 获取二维码
    getGrcode(state, QRcode) {
      state.QRcode = QRcode
    },
    // 等待扫码
    waitScanCode(state) {
      if (!state.ScanCodeShow) {
        state.tipsText = '等待用户扫码'
      }
    },
    // 扫码超时
    modeifyScanTip(state) {
      if (!this.successShow) {
        state.tipsText = '二维码失效';
        state.overtimeShow = true;
      }
    },
    // 扫码成功
    scanSuccess(state, res) {
      localStorage.setItem("id", JSON.parse(res).id);
      state.successShow = true;
    },
    //  已扫码  待确认
    ScanCodeing(state) {
      state.ScanCodeShow = true;
      state.tipsText = "您已扫码，请在手机上确认登录！"
    },
    // 移动端授权成功
    AuthorizedWebSuccess(state, res) {
      state.WapSuccess = res
    },
    // 二维码失效后，点击重新获取二维码
    AgainScanCode(state) {
      state.overtimeShow = false;
      state.ScanCodeShow = false;
    }
  },
  actions: {
    // 事务列表 ==========================
    getList(context) {
      axios.get('http://localhost:8080/list.json').then(({ data }) => {
        // console.log(data)
        context.commit('initList', data)
      })
    },
    // 扫码登录请求 ============================
    getQRcode(context) {
      socket.emit('getQRcode');
    },
    checkScanCode(context, uuid) {
      socket.emit('checkScanCode', uuid)
    }
  },
  getters: {
    // 统计未完成的任务的条数
    unDoneLength(state) {
      return state.list.filter(x => x.done === false).length
    },
    infolist(state) {
      if (state.viewKey === 'all') {
        return state.list
      }
      if (state.viewKey === 'undone') {
        return state.list.filter(x => !x.done)
      }
      if (state.viewKey === 'done') {
        return state.list.filter(x => x.done)
      }
      return state.list
    }
  }
})